<?php
namespace App\Http\Controllers\User;

use App\Models\Feedback;
use App\Models\FeedbackDetail;
use App\Models\FeedbackSetDetail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use App\Models\User;
use App\Http\Controllers\User\UserBaseController;
use App\Models\Group;

class FeedbackController extends UserBaseController
{
    
    public function __construct() {
        parent::__construct();
    }
    

    //意见反馈首页
    public function getIndex(Request $request)
    {
        $year_param = date('Y');
        /*$groups = Group::with(['users'=>function($query){
            $query->where('status','1')->orderBy('is_leader','desc')->orderByRaw('CONVERT (kh_users.qw_name USING gbk)');
        }])->where('corpid',$this->corpid)->get();*/


        $groups = Group::where('corpid', $this->corpid)->get();
        if($groups->count() <= 0){
            abort('403','没有分组，请先创建分组');
        }
        foreach ($groups as $gt) {
            $gt->users = User::where('group_id', $gt->id)->where('status', '1')->orderBy('is_leader','desc')->orderByRaw('CONVERT (kh_users.qw_name USING gbk)')->get();
        }
        $g_collect = collect();
        foreach ($groups as $g) {
            if ($g->fid == 0) {
                $g_collect[] = $g;
                foreach ($groups as $gg) {
                    if ($gg->fid == $g->id) {
                        $g_collect[] = $gg;
                        foreach ($groups as $ggg) {
                            if ($ggg->fid == $gg->id) {
                                $g_collect[] = $ggg;
                            }
                        }
                    }
                }
            }
        }


        $feed = Feedback::where('year_param',$year_param)->where('corpid',$this->corpid)->get();
        if(count($feed) < 5){
            return '没有找到'.$year_param.'年的月度意见设置';
        }
        foreach ($feed as $f){
            $f->set = FeedbackSetDetail::where('set_id',$f->id)->get();
        }
        $fcount = count($feed);

        $view = 'frontuser.month-option';
        return view($view)
            ->with('fcount',$fcount)
            ->with('groups',$g_collect)
            ->with('feedback',$feed);
    }


    //评价某员工
    public function postStoreFeedback(Request $request)
    {
        $result = array(
            'status'        => 'success',
            'msg'           => 'ok',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $score_array = $request->input('vals');
        $feedids = $request->input('feedids');
        $feedids = json_decode($feedids);
        $score_array = json_decode($score_array);
        //dd($feedids,$score_array);
        if(count($score_array) < 5){
            $result['status'] = 'fail';
            $result['msg'] = '评价项目及分值有误';
            return Response::json($result);
        }
        $to_user_id = $request->input('touserid',0);
        if($to_user_id <= 0){
            $result['status'] = 'fail';
            $result['msg'] = 'to_user_id无效';
            return Response::json($result);
        }
        $to_user = User::find($to_user_id);
        if($to_user->corpid != $this->corpid){
            $result['status'] = 'fail';
            $result['msg'] = '您没有权限评价该员工';
            return Response::json($result);
        }
        //不能评价自己
        if($to_user_id == $this->user->id){
            $result['status'] = 'fail';
            $result['msg'] = '不能评价自己';
            return Response::json($result);
        }
        foreach ($score_array as $k=>$n){
            if(!isset($feedids[$k])){
                $result['status'] = 'fail';
                $result['msg'] = '评价参数有误';
                return Response::json($result);
            }
            if(!isset($n)){
                $result['status'] = 'fail';
                $result['msg'] = '分值不能为空';
                return Response::json($result);
            }
            //每月一次
            $date = date('Y-m',time());
            $exist = FeedbackDetail::where('corpid',$this->corpid)->where('from_user_id',$this->user->id)->where('to_user_id',$to_user_id)
                ->where('feedback_id',$feedids[$k])->where('created_at','like',$date.'%')->first();
            if(!empty($exist)){
                $result['status'] = 'fail';
                $result['msg'] = '本月您已评价过该员工，请下月再评价';
                return Response::json($result);
            }
            $n = trim($n);
            $feedback = new FeedbackDetail();
            $feedback->corpid = $this->corpid;
            $feedback->feedback_id = $feedids[$k];
            $feedback->year_param = date('Y');
            $feedback->month_param = date('Ym');
            $feedback->set_detail_id = $n;
            $feedback->from_user_id = $this->user->id;
            $feedback->to_user_id = $to_user_id;
            $feedback->save();
        }

        $result['msg'] = '评价成功';
        return Response::json($result);

    }

    



   
}


